package com.zygame.sdk.view.dialog;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.zygame.sdk.ConstantValue;
import com.zygame.sdk.ZyGameSDK;
import com.zygame.sdk.bean.UserBean;
import com.zygame.sdk.bean.ZY_Toolbar;
import com.zygame.sdk.db.UserDao;
import com.zygame.sdk.model.UserModel;
import com.zygame.sdk.utils.LoginUtil;
import com.zygame.sdk.utils.SharedPreferencesHelper;
import com.zygame.sdk.utils.http.HttpUtil;
import com.zygame.sdk.utils.http.NetHttpUtil.DataCallback;
import com.zygame.sdk.utils.log.LogHelper;
import com.zygame.sdk.utils.res.ResourceUtil;
import com.zygame.sdk.view.web.ZY_Act_WebView;

import org.json.JSONObject;

/**
 * Yuxl
 * 已登录界面
 * */

public class ZY_Dialog_AccountCenter extends ZY_BaseDialog {

	private TextView tv_account,tv_change;
	private UserBean ub;
	private Context context;
	private String mAccount;
	private String mPwd;
	private String mOther;

	public ZY_Dialog_AccountCenter(Context context) {
		super(context);
		this.context = context;

	}

	@Override
	protected void init() {
		showClose(true);
		tv_account = (TextView) findViewById(ResourceUtil.getId(mContext,"tv_account"));
		tv_change = (TextView) findViewById(ResourceUtil.getId(mContext,"tv_change"));
		findViewById(ResourceUtil.getId(mContext,"rl_user")).setOnClickListener(this);
		findViewById(ResourceUtil.getId(mContext,"btn_enter_game")).setOnClickListener(this);
        UserDao ud = new UserDao();
        ub = ud.query(ud._LAST_LOGIN_FLAG, "1");
        if (ub != null) {
            mAccount = ub.userName;
            mPwd = ub.pwd;
			mOther = ub.other;
            tv_account.setText(ub.userName);

        }
		if(UserModel.getInstance().getLogin_Token() == null){

			HttpUtil.login(ub.userName, ub.pwd, "", ub.type,mOther, mContext, onLoginCallback);
		}

		tv_change.setOnClickListener(this);





	}

	@Override
	public void show() {
		super.show();
		if(UserModel.getInstance().getLogin_Token() == null){

			showLoad(true);
		}
	}

	DataCallback<JSONObject> onLoginCallback = new DataCallback<JSONObject>() {

		@Override
		public void callbackSuccess(JSONObject paramObject) {
			showLoad(false);
			LogHelper.i("login接口", paramObject.toString());
			String code = paramObject.optString("code");
			if(code.equals("0")){

				LoginUtil.loginResult(mAccount, mPwd, paramObject,mContext,"",0,"");
			}else {
				dismiss();
				Toast.makeText(mContext,paramObject.optString("message"),Toast.LENGTH_SHORT).show();
				Zy_Dialog_main dialog_main =  Zy_Dialog_main.getInstance(mContext);
				dialog_main.show();
			}
//			if (paramObject.optString("code").equals("0")) {
//				new ZY_Dialog_AccountCenter(mContext).show();
//
//			} else {
//				new ZY_Dialog_Login(mContext).show();
//			}
		}

		@Override
		public void callbackError(String error) {
//			new Login(getContext()).show();
		}
	};


	@Override
	protected String getResourceStr() {
		return "zy_dialog_account";
	}



	@Override
	public void onClick(View v) {
		super.onClick(v);
		int id = v.getId();
		if(id == ResourceUtil.getId(mContext,"tv_change")){
//			ZyGameSDK.getInstance().logout();
			ConstantValue.ZY_SHOWBACK = true;

			Zy_Dialog_main dialog_main =  Zy_Dialog_main.getInstance(context);
			dialog_main.show();

			this.dismiss();
		}else if(id == ResourceUtil.getId(mContext,"rl_user")){
			SharedPreferencesHelper sp = SharedPreferencesHelper.getInstance();
			String url = "http://wap.zy1game.com?"+"?cid="+ConstantValue.ZY_CID+"&sid="+ConstantValue.ZY_SID+"&gid="+ConstantValue.ZY_GAMEID
					+"&language="+ConstantValue.ZY_LANGUAGE+"&token="+sp.getToken(mContext)+"&login_token="+sp.getLogin_Token(mContext);;
			ZY_Toolbar main = new ZY_Toolbar(url,"",-1);
			Intent intent = new Intent(getContext(), ZY_Act_WebView.class);
			intent.putExtra(ConstantValue.TOOLBARFLAG, main);
			getContext().startActivity(intent);
		}else if(id == ResourceUtil.getId(mContext,"btn_enter_game")){
			ZyGameSDK.getInstance().onLoginResult(UserModel.getInstance());
			this.dismiss();
		}
	}



	public static  void showDia(Context context){
		ZY_Dialog_AccountCenter zy_dialog_accountCenter = new ZY_Dialog_AccountCenter(context);
		zy_dialog_accountCenter.show();

	}
}
